﻿using Niunan.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace Niunan.VideoSys.Web.Areas.Adnn1n.Controllers
{
    public class VideoController : Controller
    {
        DAL.VideoDAL dal = new DAL.VideoDAL();
        // GET: Adnn1n/Video
        public ActionResult Index(string key,int pagesize = 10, int pageindex = 1)
        {
            string cond = "1=1";
            if (!string.IsNullOrEmpty(key))
            {
                key = Tool.GetSafeSQL(key);
                cond += $" and title like '%{key}%'";
            }
            int total = dal.CalcCount(cond);
            List<Model.Video> list = dal.GetListArray("*", "id desc", pagesize, pageindex, cond);
            PagedList<Model.Video> list2 = new PagedList<Model.Video>(list, pageindex, pagesize, total);
            return View(list2);
        }

        public ActionResult Delete(string ids) { 
        
            int success = 0;
            string[] ss = ids.Split(',');
            DAL.VideoDAL dal = new DAL.VideoDAL();
            foreach (var item in ss)
            {
                int x;
                if (int.TryParse(item, out x))
                {
                    dal.Delete(x);
                    success++;
                }
            }
            return Content("成功删除" + success + "条记录！");
        }

        public ActionResult Add(int? id) {
            Model.Video model = new Model.Video();
            if (id!=null)
            {
                model = dal.GetModel(id.Value);
            }
            return View(model);
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult Add(Model.Video model) {
            if (model.id==0)
            {
                dal.Add(model);
            }
            else
            {
                dal.Update(model);
            }
            return Redirect("/Adnn1n/Video/");
        }
    }
}